<?php
/**
 * Determines the winner of the week.
 * @access public
 * @param integer $count
 * @return array
 * @author DartVader
 */
function getKwWinners($count = 10) {
    global $mysqli;

    $dateString = "";
    $kwWinners = array();
    $res = $mysqli->query("select * from view_score_kw_winner ORDER BY year DESC, kw DESC , score_abs DESC");
    while (($row=$res->fetch_assoc()) != false) {
        if ($dateString != $row['year']."-".$row['kw']) {
            $dateString = $row['year']."-".$row['kw'];
            $count--;
        }

        if ($row['kw'] < 10) {
            $row['kw'] = "0" . $row['kw'];
        }

        if (!array_key_exists($row['year'], $kwWinners)) {
           $kwWinners[$row['year']] = array();
        }

        if (!array_key_exists($row['kw'], $kwWinners[$row['year']])) {
            $kwWinners[$row['year']][$row['kw']] = array();
        }

        if (!array_key_exists('max_games', $kwWinners[$row['year']][$row['kw']])) {
            $kwWinners[$row['year']][$row['kw']]['max_games'] = getMaxGames($row['year'],$row['kw']);
        }

        if (!array_key_exists('score_abs', $kwWinners[$row['year']][$row['kw']])) {
            if (played_requied_games($kwWinners[$row['year']][$row['kw']]['max_games'],$row['count_games'])) {
                $kwWinners[$row['year']][$row['kw']] = array (
                    "firstname" => $row['firstName'],
                    "lastname" => $row['lastName'],
                    "score_abs" => $row['score_abs'],
                    "player_id" => $row['player_id'],
                    "kw" => $row['kw'],
                    "year" => $row['year'],
                    "count_games" => $row['count_games']
                );
            }
        } elseif ($kwWinners[$row['year']][$row['kw']]['score_abs'] < $row['score_abs']) {
            if (played_requied_games($row['year'],$row['kw'],$row['count_games'])) {
                $kwWinners[$row['year']][$row['kw']] = array (
                    "firstname" => $row['firstName'],
                    "lastname" => $row['lastName'],
                    "score_abs" => $row['score_abs'],
                    "player_id" => $row['player_id'],
                    "count_games" => $row['count_games']
                );
            }
        }
        if ($count == 0) {
            break;
        }
    }

    return $kwWinners;
}

/**
 * Determines the number of games.
 * @access public
 * @param integer $year
 * @param integer $kw
 * @return integer
 * @author DartVader
 */
function getMaxGames($year,$kw) {
    global $mysqli;

    $count = 0;
    $res = $mysqli->query("select max(count_games) as anzahl from view_score_kw_winner where year = '$year' and kw = '$kw'");
    if (($row=$res->fetch_assoc()) != false) {
        $count = ($row['anzahl']);
    }
    return $count;
}

/**
 * Decides whether enough games are available.
 * @access public
 * @param integer $maxCount
 * @param integer $gamesCount
 * @return boolean
 * @author DartVader
 */
function played_requied_games ($maxCount,$gamesCount) {
    global $config,$mysqli;

    if ($maxCount * $config['required_games'] <= $gamesCount) {
        return true;
    }
    return false;
}

/**
 * Description.
 * @access public
 * @param integer $width
 * @param integer $height
 * @param string $labelData
 * @param string $valueData
 * @param string $divId
 * @param string $class
 * @param string $style
 * @return string
 * @author DartVader
 */
function buildPieChart($width,$height,$labelData,$valueData,$divId,$class,$style = "") {
    $content = "";
    $content .= '<div id="'.$divId.'" class="'.$class.'" style="height:'.$height.'px;width:'.$width.'px;'.$style.'"></div>';
    $content .= '<script type="text/javascript">';

    $trenner = "";
    $contentData = "";
    for ($i=0;$i < count($valueData);$i++) {
        $contentData .= $trenner."['".$labelData[$i]."',".$valueData[$i]."]";
        $trenner = ",";
    }
    //$content.=$valueData;
    $content.=$contentData;
    $content .= "
    $(document).ready(function(){
      var plot1 = jQuery.jqplot ('$divId', [[$contentData]],
        {
          seriesDefaults: {
            // Make this a pie chart.
            renderer: jQuery.jqplot.PieRenderer,
            rendererOptions: {
                // Turn off filling of slices.
                showDataLabels: true,
                // Add a margin to seperate the slices.
                sliceMargin: 10,
                fill: false,
                // stroke the slices with a little thicker line.
                lineWidth: 5,
                shadowOffset: 2,
                shadowDepth: 5,
                shadowAlpha: 0.07
            }
          },
          legend: { show:true, location: 's' }
        }
      );
    });</script>
        ";
    return $content;
}

function getVictoryCount($placement = 5) {
    global $mysqli;

    $reqStr = "SELECT max,id FROM view_games_data_max_score LIMIT 1000000";
    $res = $mysqli->query($reqStr);
    $playerVictoryCount = array();
    while (($row=$res->fetch_assoc()) != false) {
        $reqStr1 = "SELECT player_id FROM game_data WHERE score = ".$row['max'] . " and game_id = ".$row['id'];
        $res1 = $mysqli->query($reqStr1);
        if (($row1 = $res1->fetch_assoc()) != false) {
            if (!array_key_exists($row1['player_id'], $playerVictoryCount)) {
                $playerVictoryCount[$row1['player_id']] = 0;
            }
            $playerVictoryCount[$row1['player_id']]++;
        }
    }

    var_dump($playerVictoryCount);
}
?>